package com.iflytek.management.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.iflytek.management.mapper.SysUserMapper;
import com.iflytek.management.pojo.SysUser;
import com.iflytek.management.util.MybatisUtil;

@Controller
public class LoginController {
	
	@RequestMapping(value = "/HomePage",method = RequestMethod.POST)
	public ModelAndView login(String username, String password, HttpSession sess) throws IOException {
		SysUserMapper sys_userMapper = MybatisUtil.Prepare(SysUserMapper.class);
		ModelAndView mv = new ModelAndView();
		int i = 0;
		try {
			 List<SysUser> sys_user = sys_userMapper.selectAll();
			 for (SysUser t:sys_user) {
				if (t.getUsername().equals(username)&&
						t.getPassword().equals(password)&&
						t.getIsdelete()!=1) {
					sess.setAttribute("user", t);
					i+=1;
					mv.setViewName("jsp/main.jsp");
					break;
				}				
			}
		} catch (Exception e) {
			System.out.println(e);
			mv.setViewName("login.jsp");
		}
		if(i==0) mv.setViewName("login.jsp");
		return mv;
	}
}
